<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="user">
			<el-input v-model="form.user" prefix-icon="el-icon-user" clearable
					  :placeholder="$t('login.userPlaceholder')">
				<!--				<template #append>-->
				<!--					<el-select v-model="userType" style="width: 130px;">-->
				<!--						<el-option :label="$t('login.admin')" value="admin"></el-option>-->
				<!--						<el-option :label="$t('login.user')" value="user"></el-option>-->
				<!--					</el-select>-->
				<!--				</template>-->
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password
					  :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		<!--		<el-form-item style="margin-bottom: 10px;">-->
		<!--				<el-col :span="12">-->
		<!--					<el-checkbox :label="$t('login.rememberMe')" v-model="form.autologin"></el-checkbox>-->
		<!--				</el-col>-->
		<!--				<el-col :span="12" class="login-forgot">-->
		<!--					<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>-->
		<!--				</el-col>-->
		<!--		</el-form-item>-->
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn')
				}}
			</el-button>
		</el-form-item>
		<!--		<div class="login-reg">-->
		<!--			{{$t('login.noAccount')}} <router-link to="/user_register">{{$t('login.createAccount')}}</router-link>-->
		<!--		</div>-->
	</el-form>
</template>

<script>
	import menu from "@/api/model/menu";
	import {getToken, getUserInfo} from "@/api/model/login";
	import {permissions} from "@/api/model/user";

	export default {
		data() {
			return {
				userType: 'admin',
				form: {
					user: "",
					password: "",
					autologin: false
				},
				rules: {
					user: [
						{required: true, message: this.$t('login.userError'), trigger: 'blur'}
					],
					password: [
						{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
					]
				},
				islogin: false,
			}
		},
		watch: {
			userType(val) {
				if (val == 'admin') {
					this.form.user = 'admin'
					this.form.password = 'admin'
				} else if (val == 'user') {
					this.form.user = 'user'
					this.form.password = 'user'
				}
			}
		},
		mounted() {

		},
		methods: {
			login: async function () {

				var validate = await this.$refs.loginForm.validate().catch(() => {
				})
				if (!validate) {
					return false
				}

				this.islogin = true
				await getToken({account: this.form.user, password: this.form.password}).then((user) => {
					console.log(user)
					if (user.code === 200) {
						this.$TOOL.cookie.set("TOKEN", user.data.token, {
							expires: this.form.autologin ? 24 * 60 * 60 : 0
						})
						this.$message.success("登录成功")

					} else {
						this.islogin = false
						this.$message.warning(user.msg)
						return false
					}
				})
				if (!this.islogin) return;
				//获取菜单
				let flag = true;
				await permissions().then(res => {
					let path = 'vab/mbfqdwfrzsxx';
					menu.jkdyChildren.forEach((e) => {
						if (res.data.apiPermissions && res.data.apiPermissions.length > 0) {
							let title = res.data.apiPermissions[0].name;
							path = menu.jkdyChildren.filter(jks =>
								jks.meta.title === title
							)[0].path
							res.data.apiPermissions.forEach(j => {
								if (j.name === e.meta.title) {
									menu.jkdy.children.push(e)
								}
							})
						} else {
							flag = false;
							return false;
						}
					})
					if (!flag) {
						this.$message.warning("该用户所在的部门暂未配置授权接口，请联系管理员!");
					}
					getUserInfo().then(res => {
						let adminFlag;
						adminFlag = res.data.adminFlag
						if (adminFlag) {
							menu.menu.data.menu[0].children[0] = menu.sysManager;
							menu.menu.data.menu[0].children[1] = menu.jkManager;
							menu.menu.data.menu[0].children[2] = menu.jkdy;
						} else {
							menu.menu.data.menu[0].children[0] = menu.jkdy;
						}
						this.$TOOL.data.set("MENU", menu.menu.data.menu)
						this.$TOOL.data.set("PERMISSIONS", menu.menu.data.permissions)
						this.$TOOL.data.set("DASHBOARDGRID", menu.menu.data.dashboardGrid)

						// getMenu();

						if (adminFlag) {
							this.$router.replace({
								path: '/setting/user'
							})
						} else {
							this.$router.replace({
								path: path
							})
						}
						this.islogin = false
					})
				})
			},
		}
	}
</script>

<style>
</style>
